<?php ob_start();if(!isset($_SESSION)){
session_start();
}
require_once('Connections/microassur.php');require_once('utils/fonctions.php') ;
require_once('../../../../include/inc_html_fonctions.php');
require_once('../../../../include/inc_config.php');?><?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}
$colname_rs_mod = "-1";
if (isset($_GET['Identifiant_refext'])) {
  $colname_rs_mod = $_GET['Identifiant_refext'];
}
if(isset($_GET['supp']))
{
mysql_query("START TRANSACTION");
$rq=mysql_query("delete  from referenceexterne where Identifiant_refext='".$colname_rs_mod."'");
if($rq)
{
mysql_query("commit"); 
}
else mysql_query("rollback"); 
}


$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

$bool=verifLibelle('Valeur_refext');
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1") && $bool) {
$idpers=-1;
$requete=mysql_query("select * from role where Type_role='S' and Identifiant_contrat='".$_SESSION['idcontrat']."'")or die(mysql_error());
if($requete && mysql_num_rows($requete))
{
$reponse=mysql_fetch_array($requete);
$idpers=$reponse['Identifiant_personne'];
$role=$reponse['Identifiant_role'];
}
  $insertSQL = sprintf("INSERT INTO referenceexterne (Identifiant_refext, Type_refext, Valeur_refext, Identifiant_personne, Identifiant_role, Identifiant_contrat) VALUES (%s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['Identifiant_refext'], "int"),
                       GetSQLValueString($_POST['Type_refext'], "text"),
                       GetSQLValueString($_POST['Valeur_refext'], "text"),
                       GetSQLValueString('', "int"),
                       GetSQLValueString($role, "int"),
                       GetSQLValueString($_SESSION['idcontrat'], "int"));

  mysql_select_db($database_microassur, $microassur);
  mysql_query("START TRANSACTION");
  $Result1 = mysql_query($insertSQL, $microassur) or die(mysql_error());

 if(!$Result1){ mysql_query("rollback"); 
 	  $message=colorer("Problème création référence externe, contacter la DSI ",true);

	 // header("location:index.php?ajout=echec&Identifiant_personne=".$_GET['Identifiant_personne']);
   }
  else { mysql_query("commit");  //header("location:index.php?ajout=success&Identifiant_personne=".$_GET['Identifiant_personne']);
  }
}
elseif ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1") && !$bool) {
if(empty($_POST['Valeur_refext']))$message=colorer("Vous devez saisir la valeur de la référence externe","true");
elseif(!verifNumeric($_POST['Valeur_refext']))$message=colorer("La valeur de la référence externe saisie comporte des caractères spéciaux interdits ","false");
}
elseif ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form2") &&  $bool) {
  $updateSQL = sprintf("UPDATE referenceexterne SET Type_refext=%s, Valeur_refext=%s WHERE Identifiant_refext=%s",
                       GetSQLValueString($_POST['Type_refext'], "text"),
                       GetSQLValueString($_POST['Valeur_refext'], "text"),                   
                       GetSQLValueString($_POST['Identifiant_refext'], "int"));

  mysql_select_db($database_microassur, $microassur);
  mysql_query("START TRANSACTION");
  $Result1 = mysql_query($updateSQL, $microassur) or die(mysql_error());
if(!$Result1){  mysql_query("rollback");
	  $message=colorer("Problème modification référence externe, contacter la DSI ",true);
   }
   else mysql_query("commit");// header("location:index.php?mod=success&Identifiant_personne=".$_GET['Identifiant_personne']);
  }
  elseif ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1") && !$bool) {
if(empty($_POST['Valeur_refext']))$message=colorer("Vous devez saisir la valeur de la référence externe","true");
elseif(!verifLibelle($_POST['Valeur_refext'])) $message=colorer("La valeur de la référence externe saisie comporte des caractères spéciaux interdits ","false");
}


$colname_rs_mod = "-1";
if (isset($_GET['Identifiant_refext'])) {
  $colname_rs_mod = $_GET['Identifiant_refext'];
}
mysql_select_db($database_microassur, $microassur);
$query_rs_mod = sprintf("SELECT * FROM referenceexterne WHERE Identifiant_refext = %s", GetSQLValueString($colname_rs_mod, "int"));
$rs_mod = mysql_query($query_rs_mod, $microassur) or die(mysql_error());
$row_rs_mod = mysql_fetch_assoc($rs_mod);
$totalRows_rs_mod = mysql_num_rows($rs_mod);
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Référence externe</title>
			<link rel="stylesheet" type="text/css" href="../../../../css/main.css" />
            <link rel="stylesheet" type="text/css" href="list.css" />
			<!--[if IE 7]>
            	<link rel="stylesheet" type="text/css" href="../../../../css/ie7.css" />
            	<link rel="stylesheet" type="text/css" href="form_ie7.css" />
            <![endif]--></head>

<body>
<div id="sf_menu1">
<h3>Production </h3>
<?php
	

	if(isset($message) && !empty($message)){
		echo $message;
	}
	$nommodule="Modification administrative";
	//$nommodule2="Liste des adresse";
	$nom_module_saisie="D&eacute;tail r&eacute;f&eacute;rence externe";
?>
</div>
<div class="cleared"></div>

<div class="corps1">
<div class="cleared"></div>
	<?php if(!isset($_GET['Identifiant_refext'])){?>
<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
 <div class="imgleft"></div>
	<div class="imgmiddle">
    <div class="btn">
    	<span class="btnleft"></span>
        <span class="btnmiddle">
        	<h5><?php echo $nommodule; ?></h5>
        </span><span class="btnright"></span>
    </div>
  	<div class="btn1">
    	<span class="btnleft"></span>
        <span class="btnmiddle">
        	<h5><?php echo $nom_module_saisie; ?></h5>
        </span>
        <span class="btnright"></span>
    </div>
	<div class="formulaire">
  <table class="tabform">
  
    <tr>
      <td >TYPE REFERENCE </td>
      <td>
      <select name="Type_refext" id="Type_refext"  >
      <?php $requete=mysql_query("select * from dictionnaire,elementdico where dictionnaire.Code_table='T0016' and dictionnaire.Identifiant_table=elementdico.Identifiant_table") or die(mysql_error());
	 
	  while($reponse=mysql_fetch_array($requete)) { ?>
        <option <?php  if(isset($_SESSION['Type_refext'])) echo remind_select1('Type_refext', $reponse["Code_element"]) ; else echo remind_select('Type_refext',$reponse["Code_element"]);?> value="<?php echo $reponse["Code_element"];?>"><?php echo $reponse["Nom_element"];?></option>
        <?php } ?>
        </select>
        
      
          </td>
    </tr>
    <tr >
      <td>VALEUR</td>
      <td><input type="text"  maxlength="30" name="Valeur_refext" value="<?php if(isset($_POST['Valeur_refext'])) echo $_POST['Valeur_refext']; ?>" size="32" /></td>
    </tr></table>
    </div>
    
 </div>

<div class="imgright"></div>				
<div class="cleared"></div>
     <div class="bt1"> <a href="index.php?Identifiant_personne=<?php echo $_GET['Identifiant_personne']; ?>"><span class="btleft"></span>
        <span class="btmiddle">Annuler</span>
        <span class="btright"></span>
    </a></div>
    
      <div class="bt">
	<span class="btleft"></span><input type="submit" value="Valider" class="resizeall btmiddle"/>
    <span class="btright"></span></div>
    
	

  <input type="hidden" name="Identifiant_refext" value="" />
  <input type="hidden" name="Identifiant_contrat" value="" />
  <input type="hidden" name="MM_insert" value="form1" />
</form>
<?php }
else
{ ?>
<form action="<?php echo $editFormAction; ?>" method="post" name="form2" id="form2">
<div class="imgleft"></div>
	<div class="imgmiddle">
    <div class="btn">
    	<span class="btnleft"></span>
        <span class="btnmiddle">
        	<h5><?php echo $nommodule; ?></h5>
        </span><span class="btnright"></span>
    </div>
  	<div class="btn1">
    	<span class="btnleft"></span>
        <span class="btnmiddle">
        	<h5><?php echo $nom_module_saisie; ?></h5>
        </span>
        <span class="btnright"></span>
    </div>
	<div class="formulaire">
  <table class="tabform">  
    <tr >
      <td >TYPE REFERENCE</td>
      <td> <select name="Type_refext" id="Type_refext"  >
      <?php $requete=mysql_query("select elementdico.* from dictionnaire,elementdico where dictionnaire.Code_table='T0016' and dictionnaire.Identifiant_table=elementdico.Identifiant_table") or die(mysql_error());
	 
	  while($reponse=mysql_fetch_array($requete)) {?>
        <option <?php  echo remind_select2($row_rs_mod['Type_refext'], $reponse["Code_element"]) ; ?> value="<?php echo $reponse["Code_element"];?>"><?php echo $reponse["Nom_element"];?></option>
        <?php }?>
        </select>
      </td>
    </tr>
    <tr>
      <td>VALEUR</td>
      <td><input type="text" maxlength="30" name="Valeur_refext" value="<?php echo htmlentities($row_rs_mod['Valeur_refext'], ENT_COMPAT, 'utf-8'); ?>" size="32" /></td>
    </tr></table>
    </div>
    </div>
<div class="imgright"></div>				
<div class="cleared"></div>
    
     <div class="bt1"> <a href="index.php?Identifiant_personne=<?php echo $_GET['Identifiant_personne']; ?>"><span class="btleft"></span>
        <span class="btmiddle">Annuler</span>
        <span class="btright"></span>
    </a></div>
     
     <?php if(!isset($_GET['supp'])){?>
      <div class="bt">
	<span class="btleft"></span><input type="submit" value="Valider" class="resizeall btmiddle"/>
    <span class="btright"></span></div>
    <?php } else {?><div class="bt1"> <a href="#"><span class="btleft"></span>
        <span class="btmiddle">Annuler</span>
        <span class="btright"></span>
    </a></div>
    <?php }?>
    
	<input type="hidden" name="Identifiant_refext" value="<?php echo htmlentities($row_rs_mod['Identifiant_refext'], ENT_COMPAT, 'utf-8'); ?>" />
  <input type="hidden" name="Identifiant_contrat" value="<?php echo htmlentities($row_rs_mod['Identifiant_contrat'], ENT_COMPAT, 'utf-8'); ?>" />
  <input type="hidden" name="MM_update" value="form2" />
   
</form>

<?php } ?>
</div>
</body>
</html>
<?php
mysql_free_result($rs_mod);
?>
